package lgs.hm.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import lgs.hm.model.Books;
import lgs.hm.service.BooksService;

@Controller
@RequestMapping("books")
public class BooksController {
	@Resource
	private BooksService booksService;
	
	@GetMapping("select")
	public String select(Map<String, Object> map) {
		List<Books> books = booksService.select();
		System.out.println("+++++++++++++++"+books);
		map.put("list", books);
		return "books";
	}
	
	@GetMapping("update")
	public String update(Integer id,Integer oldid,
			Books books,Map<String, Object>map) {
		if(oldid==null) {
			map.put("id", id);
			return "updatebooks";
		}
		books.setId(oldid);
		booksService.update(books);
		return "redirect:select";
	}
	
	@GetMapping("add")
	public String add(Books books) {
		if(books.getPrice()==null) {
			return "addbooks";
		}
		booksService.add(books);
		return "redirect:select";
	}
	
	@GetMapping("delete")
	public String delete(@RequestParam("id")Integer id) {
		Books books = new Books();
		books.setId(id);
		booksService.delete(books);
		return "redirect:select";
	}
	

}
